package com.example.base.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.base.job.QuartzEnum;
import com.example.base.system.domain.vo.SysJobVo;
import com.example.base.utils.EnumToListConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.base.limit.IPRateLimit;
import java.util.List;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.example.base.config.PageData;
import com.example.base.system.service.SysJobService;
import com.example.base.system.domain.SysJob;
import java.io.Serializable;
import java.util.Map;

/**
 * <p>
 * 定时任务表 前端控制器
 * </p>
 *
 * @author xiong
 * @since 2024-02-07
 */

@RestController
@Api(tags = {"定时任务表的服务类"})
@RequestMapping("/sys-job")
public class SysJobController  {

        @Autowired
        private SysJobService sysJobService;

        @GetMapping("/getjobenum")
        @ApiOperation(value = "获取定时任务类型",notes = "获取定时任务类型")
        public JSONObject getJobEnumController(){
                QuartzEnum[] quartzEnum=QuartzEnum.values();
                List<Map<String,Object>> mapList=EnumToListConverter.convert(quartzEnum,"type","name","quartClazz");
                return Result.successInfo(mapList);
        }


        @PostMapping("/saveorupdate")
        @IPRateLimit(name = "SysJob-save",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "定时任务表的新增接口",notes = "定时任务表的新增接口")
        public JSONObject save(@RequestBody SysJobVo vo) throws GlobalExcetion{
                 sysJobService.saveOrUpdateSysJob(vo);
                 return Result.successInfo("保存成功");
        }

        @GetMapping("/execone")
        @ApiOperation(value = "执行一次",notes = "定时任务表的新增接口")
        public JSONObject execOneController(@RequestParam("id")Serializable id) throws GlobalExcetion {
                sysJobService.execOne(id);
                return Result.successInfo("执行成功");
        }


        @PostMapping("/update")
        @IPRateLimit(name = "SysJob-update",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "定时任务表的更新接口",notes = "定时任务表的更新接口")
        public JSONObject update(@RequestBody SysJob sysJob) throws GlobalExcetion{
                sysJobService.updateSysJobById(sysJob);
                return Result.successInfo("更新成功");
        }



        @DeleteMapping("/{id}")
        @IPRateLimit(name = "SysJob-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "定时任务表的删除接口",notes = "定时任务表的删除接口")
        public JSONObject delete(@PathVariable("id") Serializable id) throws GlobalExcetion{
                boolean flag = sysJobService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysJob-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "定时任务表的批量删除接口",notes = "定时任务表的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids) throws GlobalExcetion {
                boolean flag =  sysJobService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/getall")
        @IPRateLimit(name = "SysJob-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "定时任务表的获取全部接口",notes = "定时任务表的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
                List<SysJob> list= sysJobService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/{id}")
        @IPRateLimit(name = "SysJob-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "定时任务表的获取一个接口",notes = "定时任务表的获取一个接口")
        public JSONObject findOne(@PathVariable("id") Serializable id) throws GlobalExcetion {
                SysJob data= sysJobService.getById(id);
                return Result.successInfo(data);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysJob-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "定时任务表的分页接口",notes = "定时任务表的分页接口")
        public JSONObject findPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
                QueryWrapper<SysJob> queryWrapper = new QueryWrapper<>();
                IPage<SysJob> data=sysJobService.page(new Page<>(page, pageSize),queryWrapper);
                PageData pageData=new PageData(data.getTotal(),page,pageSize,data.getRecords());
                return Result.successInfo(pageData);
        }
}

